package com.intranet.fund.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.intranet.fund.domain.Email;
import com.intranet.fund.service.EmailService;
import com.water.dao.Page;
import com.water.domain.DataTableParameter;
import com.water.web.Tool;

@Controller
public class EmailController {

	@Autowired
	private EmailService emailService;

	@RequestMapping("/getAllEmail")
	public void getAllEmail(HttpServletResponse response,
			DataTableParameter param) {

		// 搜索条件
		String search = param.getSSearch();
		// 查询结果集
		Page page = emailService.getAllEmail(param.getPageNo(), param
				.getIDisplayLength(), search);
		List<Email> emails = page.getData();
		List<Object> list = new ArrayList<Object>();
		for (Email a : emails) {
			String status = "";
			if(a.getStatus().equals("Y")){
				status = "启用";
			}else{
				status = "禁用";
			}
			list.add(new String[] {
					"<input name=\"checkbox\" type=\"checkbox\" class=\"checkboxes\" value=\""
							+ a.getId().toString() + "\" />",
					a.getAccount(),
					a.getOwner(), status});
		}
		// 封装页面参数
		String aaData = JSONArray.fromObject(list).toString();
		JSONObject json = new JSONObject();
		json.put(DataTableParameter.SECHO, param.getSEcho());
		json.put(DataTableParameter.ITOTALRECORDS, page.getTotalCount());// 过滤前总记录数
		json.put(DataTableParameter.ITOTALDISPALYRECORDS, page.getTotalCount());// 过滤后总记录数
		json.put(DataTableParameter.AADATA, aaData);
		Tool.writeByAction(json.toString(), response);
	}

	@RequestMapping("/loadEmail")
	public ModelAndView loadEmail(HttpServletRequest request,
			@RequestParam(value = "id", required = true)
			int id, @RequestParam(value = "type", required = true)
			String type) {
		ModelAndView mav = new ModelAndView();
		Email email = emailService.getEmailById(id);
		mav.addObject("email", email);
		if (type.equals("edit")) {
			mav.setViewName("/fund/email/editEmail");
		} else {
			mav.setViewName("/fund/email/showEmail");
		}
		return mav;
	}

	@RequestMapping("/addEmail")
	public ModelAndView addEmail(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/fund/email/addEmail");
		return mav;
	}

	@RequestMapping("/saveEmail")
	public void saveEmail(HttpServletResponse response, Email email) {
		String msg = "N";
		if (email.getId() == null) {
			Email e = new Email();
			e.setAccount(email.getAccount());
			e.setOwner(email.getOwner());
			e.setStatus(email.getStatus());
			emailService.saveEmail(e);
			msg = "Y";
		} else {
			Email e = emailService.getEmailById(email.getId());
			e.setAccount(email.getAccount());
			e.setOwner(email.getOwner());
			e.setStatus(email.getStatus());
			emailService.updateEmail(e);
			msg = "Y";
		}
		JSONObject o = new JSONObject();
		o.put("Message", msg);
		o.put("ReturnUrl", msg);
		Tool.writeByAction(o.toString(), response);
	}

	@RequestMapping("/deleteEmail")
	public void deleteEmail(HttpServletResponse response,
			@RequestParam(value = "ids", required = false)
			String ids) {
		String msg = "";
		String[] id = ids.split(",");
		for (int i = 0; i < id.length; i++) {
			Email a = emailService.getEmailById(Integer
					.valueOf(id[i]));
			msg = emailService.deleteEmail(a);
		}
		Tool.writeByAction(msg, response);
	}
}
